package com.yupi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.product.ProductAddRequest;
import com.yupi.project.model.dto.product.ProductUpdateRequest;
import com.yupi.project.model.entity.Product;
import com.yupi.project.model.entity.Question;
import com.yupi.project.model.entity.Resourse;
import com.yupi.project.model.enums.FileDir;
import com.yupi.project.service.ProductService;
import com.yupi.project.mapper.ProductMapper;
import com.yupi.project.service.QuestionService;
import com.yupi.project.service.ResourseService;
import com.yupi.project.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 * @description 针对表【product(产品表)】的数据库操作Service实现
 * @createDate 2023-07-12 11:17:00
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {


    @Resource
    private ResourseService resourseService;

    @Resource
    private FileUtil fileUtil;

    @Resource
    private QuestionService questionService;

    @Override
    @Transactional
    public Long saveProduct(ProductAddRequest productAddRequest) {
//
        Product product = new Product();
        BeanUtils.copyProperties(productAddRequest, product);

        boolean save = save(product);
        if (save) {
            Long id = product.getId();
            List<MultipartFile> products = productAddRequest.getProducts();
            if (products != null && !products.isEmpty()) {
//            保存
                saveResourse(id, products);
            }
            return product.getId();
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "保存失败");
        }

//


    }

    @Override
    public Long updateProduct(ProductUpdateRequest productUpdateRequest) {
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateRequest, product);

        boolean save = updateById(product);

        if (save) {
            Long id = product.getId();
            List<MultipartFile> products = productUpdateRequest.getProducts();
            if (products != null && !products.isEmpty()) {
                deleteResourse(id);
//            保存
                saveResourse(id, products);
            }
            return id;
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该记录不存在");
        }

    }

    @Override
    public Boolean deleteProduct(Long id) {

        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Question::getProductid, id);
        long count = questionService.count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请先删除该产品下所有问题");
        }

        boolean b = removeById(id);

        if (b) {
            deleteResourse(id);
        }

        return b;

    }


    /**
     * 保存资源
     */
    private void saveResourse(Long id, List<MultipartFile> products) {
        for (MultipartFile multipartFile : products) {
            String s = fileUtil.saveMultipartFile(multipartFile, FileDir.product);
            Resourse resourse = new Resourse();
            resourse.setUrl(s);
            resourse.setProductid(id);
            resourseService.save(resourse);
        }
    }

    /**
     * 删除资源
     */
    private void deleteResourse(Long id) {
        LambdaQueryWrapper<Resourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Resourse::getProductid, id);
        List<Resourse> list = resourseService.list(queryWrapper);
        List<String> urlList = list.stream().map(Resourse::getUrl).collect(Collectors.toList());
        if (urlList != null) {
            fileUtil.removeFileList(urlList);
        }
//        删除数据库记录
        resourseService.remove(queryWrapper);
    }
}




